package gov.mmgt.netmanager.ui;

import gov.mmgt.netmanager.common.Constant;
import gov.mmgt.netmanager.common.exception.MyException;
import gov.mmgt.netmanager.operate.GetAdapterInfoByJNI;
import gov.mmgt.netmanager.operate.PropertiesOperate;
import gov.mmgt.netmanager.ui.common.MyDialog;
import gov.mmgt.netmanager.vo.NetworkInfoVO;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Control;

/**
 * 
 * Copyright (c) 2013,茂名市国土资源局<br>
 * All rights reserved.<br>
 * 
 * 文件名称：SystemSettingPanle.java<br>
 * 摘    要：系统设置面板类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：梁铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：2014-5-26<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：2014-5-26<br>
 */
public class SystemSettingPanle extends Composite {
	private Composite panle;
	private Button autoRun;
	private Button autoStart;
	private Button autoToBlackList;
	private Combo interval;
	private Text mac;
	private Text ip;
	private Text gateway;
	private Text gatewayMac;
	private Combo adapterListCombo;
	private List<NetworkInfoVO> adapterList;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SystemSettingPanle(Composite parent, int style) {
		super(parent, style);
		this.panle = parent;
		this.setBounds(0, 0, 387, 345);
		
		interval = new Combo(this, SWT.READ_ONLY);
		interval.setBounds(46, 70, 54, 23);
		
		String[] delayTimes = {"10","30","60"};
		interval.setItems(delayTimes);
		interval.select(0);
		
		CLabel label = new CLabel(this, SWT.NONE);
		label.setBounds(10, 70, 30, 23);
		label.setText("每隔");
		
		CLabel label_1 = new CLabel(this, SWT.NONE);
		label_1.setBounds(106, 70, 102, 23);
		label_1.setText("分钟扫描一次网络");
		
		autoRun = new Button(this, SWT.CHECK);
		autoRun.setBounds(10, 10, 179, 23);
		autoRun.setText("开机启动网络管理系统");
		
		autoStart = new Button(this, SWT.CHECK);
		autoStart.setBounds(248, 10, 129, 23);
		autoStart.setText("启动后自动开启监控");
		
		autoToBlackList = new Button(this, SWT.CHECK);
		autoToBlackList.setBounds(10, 41, 198, 23);
		autoToBlackList.setText("自动把异常IP添加到黑名单");
		
		Button button = new Button(this, SWT.NONE);
		
		//保存系统设置
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(autoRun.getSelection()) { //开机运行
					Constant.PARAMETER.setAutoRun("true");
				} else {
					Constant.PARAMETER.setAutoRun("false");
				}
				
				if(autoStart.getSelection()) { //启动自动开始
					Constant.PARAMETER.setAutoStart("true");
				} else {
					Constant.PARAMETER.setAutoStart("false");
				}
				
				if(autoToBlackList.getSelection()) { //自动加入黑名单
					Constant.PARAMETER.setAutoAddToBlackList("true");
				} else {
					Constant.PARAMETER.setAutoAddToBlackList("false");
				}
				
				Constant.PARAMETER.setInterval(interval.getText()); //扫描时间间隔
				Constant.PARAMETER.setAdapter(adapterListCombo.getText()); //网卡名称
				
				//保存系统参数
				PropertiesOperate proOpt = new PropertiesOperate("config.ini");
				
				try {
					proOpt.saveParameter(Constant.PARAMETER);
					MyDialog.alert(panle.getShell(), "数据源保存成功！");
				} catch (MyException e1) {
					MyDialog.alert(panle.getShell(), e1.getMessage());
				}
			}
		});
		button.setText("保存");
		button.setBounds(297, 244, 80, 25);
		
		CLabel label_2 = new CLabel(this, SWT.NONE);
		label_2.setBounds(10, 99, 51, 23);
		label_2.setText("监控网卡");
		
		adapterListCombo = new Combo(this, SWT.READ_ONLY);
		
		//获取网卡列表
		adapterList = GetAdapterInfoByJNI.getAdapterList();
				
		//选择网卡触发的操作
		adapterListCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectedIndex = adapterListCombo.getSelectionIndex();
				
				if(selectedIndex == 0) {
					mac.setText("");
					ip.setText("");
					gateway.setText("");
				} else {
					for(NetworkInfoVO adapter : adapterList) {
						if(adapterListCombo.getData(Integer.toString(selectedIndex)).equals(adapter.getName())) {
							mac.setText(adapter.getMac());
							ip.setText(adapter.getIp());
							gateway.setText(adapter.getGateway());
						}
					}
				}
			}
		});
		
		adapterListCombo.setBounds(67, 99, 310, 23);
		
		String[] adapterDescriptions = new String[adapterList.size() + 1];
		
		//设置下拉列表初始值
		adapterDescriptions[0] = "请选择监控网卡";
		adapterListCombo.setData("0","null");
		
		//设置下拉列表的键值
		for(int i = 0; i < adapterList.size(); i++) {
			adapterListCombo.setData(Integer.toString(i + 1), adapterList.get(i).getName());
			adapterDescriptions[i + 1] = adapterList.get(i).getDescription();
		}
		
		//设置下拉列表的显示
		adapterListCombo.setItems(adapterDescriptions);
		
		adapterListCombo.select(0);
		
		CLabel label_3 = new CLabel(this, SWT.NONE);
		label_3.setBounds(10, 128, 54, 21);
		label_3.setText("网卡地址");
		
		mac = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		mac.setBounds(67, 128, 310, 23);
		
		CLabel lblNewLabel = new CLabel(this, SWT.NONE);
		lblNewLabel.setBounds(10, 157, 54, 23);
		lblNewLabel.setText("IP地址");
		
		ip = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		ip.setBounds(67, 157, 310, 23);
		
		CLabel lblNewLabel_1 = new CLabel(this, SWT.NONE);
		lblNewLabel_1.setBounds(10, 186, 54, 23);
		lblNewLabel_1.setText("网关地址");
		
		gateway = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		gateway.setBounds(67, 186, 310, 23);
		
		CLabel lblmac = new CLabel(this, SWT.NONE);
		lblmac.setBounds(10, 215, 54, 23);
		lblmac.setText("网关MAC");
		
		gatewayMac = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		gatewayMac.setBounds(67, 215, 310, 23);
		setTabList(new Control[]{autoRun, autoStart, autoToBlackList, interval, adapterListCombo, button});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
